package com.oceansoft.core.jxkh.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hui.platform.system.exception.HuiException;
import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.CommonUtilCache;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.common.util.StringUtil;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.domain.SysDept;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.domain.BeChecker;
import com.oceansoft.core.jxkh.domain.Checker;
import com.oceansoft.core.jxkh.service.ITaskRelationService;
import com.oceansoft.core.service.IUUMSRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by 祝亚 on 2017/12/11.
 */
@Controller
@RequestMapping("/task/relation")
public class TasktRelationController {
    @Resource
    private ITaskRelationService taskRelationService;

    /*
    *  考核及被考核人设置
   * */
    @RequestMapping(value = "khrygxsz.html")
    public String khrygxsz(HttpServletRequest request, ModelMap model) {
        String taskId = request.getParameter("taskId");
        model.addAttribute("deptid", "-1");
        model.addAttribute("taskId", taskId);
        return "taskrelation/khrygxsz";
    }

    /*
     *  部门下考官列表Checker
    * */
    @RequestMapping(value = "checkList.json")
    public void checkList(HttpServletRequest request, ModelMap modelMap) {
//        String offset = request.getParameter("offset");
//        String limit = request.getParameter("limit");
//        String order = request.getParameter("order");
        String deptid = request.getParameter("deptid");
        String taskId = request.getParameter("taskId");
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        HashMap<String, String> paramap = new HashMap<String, String>();
//        paramap.put("offset", offset);
//        paramap.put("limit", limit);
        paramap.put("taskId", taskId);
        paramap.put("deptid", deptid);

        List<Checker> TaskPag = taskRelationService.querybycondition(paramap, user);
        modelMap.addAttribute("rows", TaskPag);
        modelMap.addAttribute("total", TaskPag.size());
    }

    /*
  *  删除考官
 * */
    @RequestMapping(value = "deleteCheck/{dbid}.json")
    public void deleteCheck(@PathVariable("dbid") String dbid, HttpServletRequest request, ModelMap model) {
        Message message = new Message("操作成功！");
        try {
            taskRelationService.deleteCheck(dbid);
            message.setStatus(Constant.RESULT_SUCC);
        } catch (HuiException e) {
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody("操作失败");
        }
        model.addAttribute("message", message);
    }

    /*
        *  考核及被考核人设置
       * */
    @RequestMapping(value = "addChecker.html")
    public String addChecker(HttpServletRequest request, ModelMap model) {
        String taskId = request.getParameter("taskId");
        String deptid = request.getParameter("deptid");
        model.addAttribute("deptid", deptid);
        model.addAttribute("taskId", taskId);
        return "taskrelation/addChecker";
    }

    /*
         *  部门下非考官考官人员列表
        * */
    @RequestMapping(value = "checkUserList.json")
    public void checkUserList(HttpServletRequest request, ModelMap modelMap) {
        String taskId = request.getParameter("taskId");
        String deptid = request.getParameter("deptid");
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("taskId", taskId);
        paramap.put("deptid", deptid);

        List<SysUser> list = taskRelationService.checkUserList(paramap, user);
        modelMap.addAttribute("rows", list);
        modelMap.addAttribute("total", list.size());
    }

    /*
        *  保存部门考官
       * */
    @RequestMapping(value = "saveChecker.json")
    public void saveChecker(HttpServletRequest request, ModelMap modelMap) {
        String taskId = request.getParameter("taskId");
        String ids = request.getParameter("ids");
        String names = request.getParameter("usernames");
        String deptid = request.getParameter("deptid");
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("taskId", taskId);
        paramap.put("ids", ids);
        paramap.put("deptid", deptid);
        if (StringUtil.isNotNullAndBlank(ids) && ids.length() > 0) {
            String ss[] = ids.split(",");
            String name[] = names.split(",");
            try {
                for (int i = 0; i < ss.length; i++) {
                    Checker checker = new Checker();
                    checker.setDeptno(deptid);
                    checker.setTaskid(taskId);
                    checker.setCheckerid(ss[i]);
                    checker.setCheckername(name[i]);
                    taskRelationService.saveChecker(checker);

                }
            } catch (HuiException e) {
                e.printStackTrace();
            }
        }
    }


    /*
    *  某考官的考核人员列表
   * */
    @RequestMapping(value = "relationList.json")
    public void relationList(HttpServletRequest request, ModelMap modelMap) {
        String checkerdbid = request.getParameter("checkerdbid");
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("checkerdbid", checkerdbid);
        List<BeChecker> TaskPag = null;
        try {
            TaskPag = taskRelationService.relationList(paramap, user);
        } catch (HuiException e) {
            e.printStackTrace();
        }
        modelMap.addAttribute("rows", TaskPag);
        modelMap.addAttribute("total", TaskPag.size());
    }

    /*
    *  删除被考核人
   * */
    @RequestMapping(value = "deleteBeCheck/{dbid}.json")
    public void deleteBeCheck(@PathVariable("dbid") String dbid, HttpServletRequest request, ModelMap model) {
        Message message = new Message("操作成功！");
        try {
            taskRelationService.deleteBeCheck(dbid);
            message.setStatus(Constant.RESULT_SUCC);
        } catch (HuiException e) {
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody("操作失败");
        }
        model.addAttribute("message", message);
    }


    /*
        *  新增被考核人
       * */
    @RequestMapping(value = "addBeChecker.html")
    public String addBeChecker(HttpServletRequest request, ModelMap model) {
        String checkerdbid = request.getParameter("checkerdbid");
        String deptid = request.getParameter("deptid");
        model.addAttribute("deptid", deptid);
        model.addAttribute("checkerdbid", checkerdbid);
        return "taskrelation/addBeChecker";
    }

    /*
       *  部门下非此考官的被考核人列表
      * */
    @RequestMapping(value = "beCheckerList.json")
    public void beCheckerList(HttpServletRequest request, ModelMap modelMap) {
        String checkerdbid = request.getParameter("checkerdbid");
        String deptid = request.getParameter("deptid");
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("deptid", deptid);
        paramap.put("checkerdbid", checkerdbid);
        List<SysUser> list = taskRelationService.beCheckerList(paramap, user);
        modelMap.addAttribute("rows", list);
        modelMap.addAttribute("total", list.size());
    }


    /*
       *  保存某考官的被考核人
      * */
    @RequestMapping(value = "saveBeChecker.json")
    public void saveBeChecker(HttpServletRequest request, ModelMap modelMap) {
        String checkerdbid = request.getParameter("checkerdbid");
        String ids = request.getParameter("ids");
        String names = request.getParameter("usernames");
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        if (StringUtil.isNotNullAndBlank(ids) && ids.length() > 0) {
            String ss[] = ids.split(",");
            String name[] = names.split(",");
            try {
                for (int i = 0; i < ss.length; i++) {
                    BeChecker checker = new BeChecker();
                    checker.setCheckerdbid(Integer.parseInt(checkerdbid));
                    checker.setBecheckerid(Integer.parseInt(ss[i]));
                    checker.setBecheckername(name[i]);
                    taskRelationService.saveBeChecker(checker);
                }
            } catch (HuiException e) {
                e.printStackTrace();
            }
        }
    }


    @RequestMapping(value = "khgxview.json")
    public void statistics(HttpServletRequest request, ModelMap modelMap) {
        String deptId = request.getParameter("deptId");
        String taskId = request.getParameter("taskId");
        if(StringUtil.isNullOrBlank(taskId)){
            taskId="495";
        }
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        Map<String, Object> roles = (Map<String, Object>) request.getSession().getAttribute(
                Constant.SESSION_ROLOES);
        boolean isadmin = roles.containsKey((String) ((ServletContext) CommonUtilCache.getItem("ctx")).getAttribute("admin_roleid"));
        List<Map<String, Object>> dept = new ArrayList<Map<String, Object>>();
        dept = taskRelationService.getFirstLevelDept(isadmin, user);
        int dataIndex1 = 0;
        int dataIndex2 = 0;
        for (int i = 0; i < dept.size(); i++) {
            //根据deptid获取当前部门下所有的考官
            List<Map<String, Object>> checker = new ArrayList<Map<String, Object>>();
            checker = taskRelationService.findCheckerByDeptidAndTaskId(dept.get(i).get("DEPTID"), taskId);
            for (int k = 0; k < checker.size(); k++) {
                //根据deptid获取当前部门下所有的考官
                List<SysUser> bechecker = new ArrayList<SysUser>();
                bechecker = taskRelationService.findBeCheckerByCheckeridAndTaskId(checker.get(k).get("dbid"), taskId);
                if(bechecker.size()>0&&dataIndex1==0){
                    dataIndex1 = i;
                    dataIndex2 = k;
                }
                checker.get(k).put("children", bechecker);
                checker.get(k).put("name", checker.get(k).get("CHECKERNAME"));
                checker.get(k).put("collapsed",false);
            }
            dept.get(i).put("children", checker);
            dept.get(i).put("name", dept.get(i).get("ALIAS"));
            dept.get(i).put("collapsed",true);
        }
        dept.get(dataIndex1).put("collapsed",false);
        JSONObject object = new JSONObject();
        object.put("name", "江苏省公安厅");
        object.put("children", dept);
        modelMap.addAttribute("data", object);
    }
}
